package wk.demo.fragments.dialogFragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.luoye.bzijkplayer.R;
import com.suke.widget.SwitchButton;
import com.tencent.mmkv.MMKV;

import wk.demo.config.WkConfig;
import wk.demo.widget.wkRadioButton.SegmentedGroup;


public class WkPlayerDetailSettingFragment extends Fragment {

    private OnItemClick onItemClick ;


    public static WkPlayerDetailSettingFragment getInstance(OnItemClick onItemClick) {
        WkPlayerDetailSettingFragment fragment = new WkPlayerDetailSettingFragment();
        fragment.onItemClick = onItemClick ;
        return fragment;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.fragment_wkcamera_other, container, false);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }


    private Button actionToggleRatio;
    //private Button action_toggle_render;
    private void  initView(View view){
        actionToggleRatio =view.findViewById(R.id.action_toggle_ratio);
        actionToggleRatio.setOnClickListener(onClickListener);

        /*action_toggle_render =view.findViewById(R.id.action_toggle_render);
        action_toggle_render.setOnClickListener(onClickListener);*/

        intSegment(view ,getActivity());

        onItemClick.onRatioClick(true);
        onItemClick.onRenderClick(true);



    }

    private void intSegment(View rootView , Activity activity){
        SegmentedGroup segmentedGroup =rootView. findViewById(R.id.speedSegment) ;
        segmentedGroup.setTintColor(Color.parseColor("#12b7f5") ,Color.parseColor("#ffffff"));
        segmentedGroup.setUnCheckedTintColor(Color.parseColor("#ffffff") , Color.parseColor("#12b7f5"));

        segmentedGroup.addButton(activity     , 1 , "0.5倍") ;
        segmentedGroup.addButton(activity     , 2 , "1.0倍");
        segmentedGroup.addButton(activity     , 3 , "1.5倍");
        segmentedGroup.addButton(activity     , 4 , "2.0倍");
        float [] speeds= {0.5f , 1.0f ,1.5f ,2.0f} ;

        segmentedGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                onItemClick.onSpeedSetChange( speeds[checkedId-1]);
            }
        }) ;
        RadioButton first = (RadioButton) segmentedGroup.getChildAt(0);
        first.setChecked(true);
    }

    public void updateRatioInfo(String msg){
        if(actionToggleRatio ==null){
            return;
        }
        actionToggleRatio.setText(msg);
    }


    /*public void updateRenderInfo(String msg){
        if(action_toggle_render==null){
            return;
        }
        action_toggle_render.setText(msg);
    }*/





    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int vId = v.getId() ;
            if(vId==R.id.action_toggle_ratio){
                onItemClick.onRatioClick(false);
            }/*else if(vId==R.id.action_toggle_render){
                onItemClick.onRenderClick(false);
            }*/

        }
    };


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }


    public interface OnItemClick {
        void onRatioClick( boolean readOnly );
        void onRenderClick(boolean readOnly);
        void onSpeedSetChange(float speed);
    }
}
